package com.mohan.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.mohan.gulimall.product.entity.CategoryBrandRelationEntity;
import com.mohan.gulimall.product.service.CategoryBrandRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mohan.common.utils.PageUtils;
import com.mohan.common.utils.Query;

import com.mohan.gulimall.product.dao.BrandDao;
import com.mohan.gulimall.product.entity.BrandEntity;
import com.mohan.gulimall.product.service.BrandService;
import org.springframework.util.StringUtils;


@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {
  @Autowired
  private CategoryBrandRelationService categoryBrandRelationService;

  @Override
  public PageUtils queryPage(Map<String, Object> params) {
    QueryWrapper<BrandEntity> wrapper = new QueryWrapper<>();
    String key = (String) params.get("key");
    wrapper.eq("brand_id",key).or().like("name",key);
    IPage<BrandEntity> page = this.page(
            new Query<BrandEntity>().getPage(params),
            wrapper
    );

    return new PageUtils(page);
  }

  @Override
  public void updateDetail(BrandEntity brand) {
    this.updateById(brand);
    if (!StringUtils.isEmpty(brand.getName())) {
      categoryBrandRelationService.updateBrand(brand.getBrandId(),brand.getName());
    }

  }

}